package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.params.PageQueryParam;
import com.itheima.common.vo.PageResult;
import com.itheima.common.vo.Result;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;

import javax.annotation.Resource;
import java.util.List;

@Service(version = "1.0")
public class CheckItemServiceImpl implements CheckItemService {

    @Resource
    private CheckItemMapper checkItemMapper;

    @Override
    public Result findPage(PageQueryParam pageQueryParam) {
        //分页查询
        PageHelper.startPage(pageQueryParam.getCurrentPage(),pageQueryParam.getPageSize());

        Page<CheckItem> checkItemPage = checkItemMapper.findByCondition(pageQueryParam.getQueryString());
        PageResult pageResult = new PageResult();
        pageResult.setList(checkItemPage.getResult());
        pageResult.setTotal(checkItemPage.getTotal());

        return Result.success(pageResult);
    }

    @Override
    public Result save(CheckItem checkItem) {
        checkItemMapper.save(checkItem);
        return Result.success(null);
    }

    @Override
    public Result findById(Integer id) {
        CheckItem checkItem = this.checkItemMapper.findById(id);
        return Result.success(checkItem);
    }

    @Override
    public Result update(CheckItem checkItem) {
        this.checkItemMapper.update(checkItem);
        return Result.success(null);
    }

    @Override
    public Result delete(Integer id) {
        /**
         * 1. 检查 此id的检查项是否已经被使用
         * 2. 如果没有被使用 才能删除
         */
        //select count(*) from t_checkgroup_checkitem where checkitem_id=#{id}
        long count = this.checkItemMapper.countByItemId(id);
        if (count > 0){
            return Result.fail(500,"检查项已经被使用");
        }
        this.checkItemMapper.delete(id);
        return Result.success(null);
    }

    @Override
    public Result findAll() {
        List<CheckItem> checkItemList = this.checkItemMapper.findAll();
        return Result.success(checkItemList);
    }
}
